#!/bin/sh
# function set for nanoRC system
#  (c) dangbinghoo@gmail.com 2011 playpenguin.org
# for more infomation, please reference to nanoRC on sourceforge
#

. /etc/conf.d/nanoRC.conf


# screen size
if [ -z "${colum_size}" ];then
	colum_size=80
fi

status_size=8 
rc_current_cusor=0

# color 
if [ ${use_color} = "yes" ];then
	NORMALC="\\033[0;39m"         # Standard console grey
	SUCCESS="\\033[1;32m"         # Success is green
	WARNING="\\033[1;33m"         # Warnings are 9ellOw
	FAILURE="\\033[1;31m"         # Failures are red
	INFOMSG="\\033[1;36m"         # InfOrmation is light cyan
	BRACKET="\\033[1;34m"         # Braccets 
	IMPORTT="\\033[1;31m"         # Important messge are red
else
	NORMALC=""         # Standard console grey
	SUCCESS=""         # Success is green
	WARNING=""         # Warnings are 9ellOw
	FAILURE=""         # Failures are red
	INFOMSG=""         # InfOrmation is light cyan
	BRACKET=""         # Braccets 
	IMPORTT=""         # Important messge are red
fi

# prompt
OK_msg="${BRACKET}[ ${SUCCESS} OK ${BRACKET} ]${NORMALC}"
DE_msg="${BRACKET}[ ${SUCCESS}DONE${BRACKET} ]${NORMALC}"
FL_msg="${BRACKET}[ ${FAILURE}Fail${BRACKET} ]${NORMALC}"
WN_msg="${BRACKET}[ ${WARNING}Warn${BRACKET} ]${NORMALC}"
SK_msg="${BRACKET}[ ${INFOMSG}Skip${BRACKET} ]${NORMALC}"
rcspace=" "

#
# rc print with specified width
#
rc_print()
{
    str="$1"
	
	# print out line by line plus status message.
	while true;do
		# str len
    		len=$((${#str}))

		# print lines with colum size
		if [ $len -gt ${colum_size} ];then
			#echo `expr substr "$str" 1 ${colum_size}`
			echo  "${str:0:${colum_size}}"
			#str=`expr substr "$str" $((colum_size+1)) $len`
			str="${str:${colum_size}:$len}"
			continue
		else
			echo -n  "$str"
			# We need this info when print a status message
			# check_status need to know where cusor is.
			rc_current_cusor=$len
			export rc_current_cusor
			break
		fi
	done
}

#
# rc status message
#
rc_status()
{ 
	if [ $((rc_current_cusor+status_size+2)) -ge ${colum_size} ];then
		echo 
		rc_current_cusor=0
	fi

	# print out spaces.
	spacecount=$((colum_size-(rc_current_cusor+status_size)))		
   	spacestr=`printf "%*s" "${spacecount}"`
    	if [  -n "${rcspace}" ];then
   	   	 spacestr=${spacestr// /$rcspace} 
   	fi
	echo -n "$spacestr"
	# print status
	echo  -e "$1"

	# update cusor position
	export rc_current_cusor
}

#
# print a sepration line
# $1 char.
rc_spline()
{
   	splinestr=`printf "%*s" "${colum_size}"`
   	splinestr=${splinestr// /$1} 
   	echo "$splinestr"
}

#
# rc echo (update curor position)
#
rc_echo()
{
	echo "$1"
	rc_current_cusor=0  
	export rc_current_cusor
}

#
# rc warning message
#
rc_warning()
{
	echo -e "${WARNING}"
	echo -n " W: "
	rc_print "$1"
	echo -e "${NORMALC}"
	rc_current_cusor=0  
	export rc_current_cusor
}

#
# rc error message
#
rc_error()
{
	echo -e "${FAILURE}"
	echo -n " E: "
	rc_print "$1"
	echo -e "${NORMALC}"
	rc_current_cusor=0 
	export rc_current_cusor
}

#
# rc notify message
#
rc_notify()
{
	echo -e "${INFOMSG}"
	echo -n " !  "
	rc_print "$1"
	echo -e "${NORMALC}"
	rc_current_cusor=0 
	export rc_current_cusor
}

#
# rc startup message
#
rc_do_msg()
{
	rc_print " * $1 "
}

#
# rc status
#
rc_print_status()
{
	if [ "${rc_daemon_status}" = "started" ];then
		rc_warning "program already started."
		rc_status  "${SK_msg}"
		return
	fi
	
	if [ "${rc_daemon_status}" = "failed" ];then
		rc_error "sevice startup failed."
		rc_status  "${FL_msg}"
		return
	fi
	
	if [ "${rc_daemon_status}" = "startok" ];then
		rc_status  "${OK_msg}"
		return
	fi
	
	if [ "${rc_daemon_status}" = "stoped" ];then
		rc_status  "${OK_msg}"
		return
	fi
}

#
# check proc command
# $1 : cmd
# return : pid if proc started , or, return null
# 
# changed ps+grep+awk grepping to pgrep cmd for performance 
#
rc_check_proc_cmd()
{
    echo `pgrep $1`
}


#
# rc start daemon
# : $1- daemon bin
# : $2- params
# : $3- sleep times
# : $4- pid command for checking pid
start_daemon()
{
	# check if we got 4 params
	if [ "$#" != "4" ];then
		rc_error "start_daemon function should have 4 params:
		           cmd_filename params sleeptime checkpid_cmdname"
	fi
	proc_pid=`rc_check_proc_cmd "$4"`
	
	if [ -n "${proc_pid}" ];then
		rc_daemon_status="started"
		export rc_daemon_status
		return
	fi 
	
	sleep $3
	eval "$1 $2 & > /dev/null 2>&1"
    # sleep 200ms to check again avoid segmentation fault check failed
    sleep 0.2
	proc_pid=`rc_check_proc_cmd "$4"`
	
	if [ -z "${proc_pid}" ];then
		rc_daemon_status="failed"
	else
		rc_daemon_status="startok"
	fi
	
	export rc_daemon_status
	return
}

#
# rc auto restart daemon
# : $1- daemon bin
# : $2- params
# : $3- sleep times
# : $4- pid command for checking pid
autorestart_daemon()
{
	# check if we got 4 params
	if [ "$#" != "4" ];then
		rc_error "start_daemon function should have 4 params:
		           cmd_filename params sleeptime checkpid_cmdname"
	fi
	proc_pid=`rc_check_proc_cmd "$4"`
	
	if [ -n "${proc_pid}" ];then
		rc_daemon_status="started"
		export rc_daemon_status
		return
	fi 
	
	sleep $3
	
	# daemon will restart when exited.	
	/etc/init.d/autorestart_helper "$1" "$2" "$3" "$4" &
	
	sleep 0.2
	proc_pid=`rc_check_proc_cmd "$4"`
	
	if [ -z "${proc_pid}" ];then
		rc_daemon_status="failed"
	else
		rc_daemon_status="startok"
	fi
	
	export rc_daemon_status
	return
}


#
# rc stop daemon
# $1: proc cmd
stop_daemon()
{
	cmd_pid=`rc_check_proc_cmd "$1"`
	kill ${cmd_pid} > /dev/null 2>&1
	
	# I just think there's no dault 
	# that a proccess can not be stoped by kill -9
	cmd_pid=`rc_check_proc_cmd "$1"`
	if [ -n "${cmd_pid}" ];then
		kill -9 ${cmd_pid} > /dev/null 2>&1
	fi
	
	rc_daemon_status="stoped"
	export rc_daemon_status
}

